package org.lwi.base;

import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.io.Serializable;

/**
 * @author pc55
 */
@Data
@ApiModel(value = "Api接口返回对象", description = "Api接口返回对象")
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 成功标志
     */
    private boolean success = true;
    /**
     * 返回处理信息
     */
    private String message = "操作成功!";
    /**
     * 返回的代码
     */
    private int code = 0;
    /**
     * 返回的数据对象
     */
    private T data;
    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public ApiResult() {

    }

    public ApiResult<T> success(String msg) {
        this.message = msg;
        this.code = CommonConstant.SC_OK_200;
        return this;
    }

    public static ApiResult<Object> ok() {
        ApiResult<Object> result = new ApiResult<Object>();
        result.setSuccess(true);
        result.setCode(CommonConstant.SC_OK_200);
        result.setMessage("成功");
        return result;
    }

    public static ApiResult<Object> ok(String msg) {
        ApiResult<Object> result = new ApiResult<Object>();
        result.setSuccess(true);
        result.setCode(CommonConstant.SC_OK_200);
        result.setMessage(msg);
        return result;
    }

    public static ApiResult<Object> ok(Object data) {
        ApiResult<Object> result = new ApiResult<Object>();
        result.setSuccess(true);
        result.setCode(CommonConstant.SC_OK_200);
        result.setData(data);
        return result;
    }

    public static ApiResult<Object> error(String msg) {
        return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
    }

    private static ApiResult<Object> error(int code, String msg) {
        ApiResult<Object> result = new ApiResult<Object>();
        result.setCode(code);
        result.setMessage(msg);
        result.setSuccess(false);
        return result;
    }

    /**
     * 无权访问返回结果
     *
     * @param msg
     * @return
     */
    public static ApiResult<Object> noAuthorize(String msg) {
        return error(CommonConstant.SC_NO_AUTHORIZE, msg);
    }

}
